package com.lishem.top100._07linkedlist;

import com.lishem.common.ListNode;

import java.util.PriorityQueue;

/**
 * https://leetcode.cn/problems/merge-k-sorted-lists/description/
 * <p>
 * 给你一个链表数组，每个链表都已经按升序排列。
 * <p>
 * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
 * <p>
 * 示例 1：
 * <p>
 * 输入：lists = [[1,4,5],[1,3,4],[2,6]]
 * <p>
 * 输出：[1,1,2,3,4,4,5,6]
 * <p>
 * 解释：链表数组如下：
 * <p>
 * [
 * 1->4->5,
 * 1->3->4,
 * 2->6
 * ]
 * <p>
 * 将它们合并到一个有序链表中得到。
 * <p>
 * 1->1->2->3->4->4->5->6
 * <p>
 * 示例 2：
 * <p>
 * 输入：lists = []
 * <p>
 * 输出：[]
 * <p>
 * 示例 3：
 * <p>
 * 输入：lists = [[]]
 * <p>
 * 输出：[]
 */
public class _13LetCode23_合并K个升序链表 {

    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> pq = new PriorityQueue<>((n1, n2) -> {
            return n1.val - n2.val;
        });
        for (ListNode head : lists) {
            while (head != null) {
                pq.add(head);
                head = head.next;
            }
        }
        ListNode tempHead = new ListNode();
        ListNode cur = tempHead;
        while (!pq.isEmpty()) {
            cur.next = pq.poll();
            cur = cur.next;
        }
        cur.next = null;
        return tempHead.next;
    }

    // 自己写的方法
    public ListNode mergeKLists1(ListNode[] lists) {
        ListNode[] heads = new ListNode[lists.length];
        for (int i = 0; i < lists.length; i++) {
            heads[i] = new ListNode(0);
            heads[i].next = lists[i];
        }
        ListNode tempHead = new ListNode(0);
        ListNode cur = tempHead;
        while (true) {
            int minIndex = -1;
            for (int i = 0; i < heads.length; i++) {
                if (heads[i].next == null) {
                    continue;
                }
                if (minIndex == -1) {
                    minIndex = i;
                } else {
                    minIndex = heads[minIndex].next.val < heads[i].next.val ? minIndex : i;
                }
            }
            if (minIndex == -1) {
                return tempHead.next;
            }
            cur.next = heads[minIndex].next;
            cur = cur.next;
            heads[minIndex].next = heads[minIndex].next.next;
        }
    }

    public static void main(String[] args) {
        _13LetCode23_合并K个升序链表 sol = new _13LetCode23_合并K个升序链表();
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(4);
        head1.next.next = new ListNode(5);

        ListNode head2 = new ListNode(1);
        head2.next = new ListNode(3);
        head2.next.next = new ListNode(4);

        ListNode head3 = new ListNode(2);
        head3.next = new ListNode(6);

        ListNode[] lists = {head1, head2, head3};
        ListNode.printListNode(sol.mergeKLists1(lists));

        System.out.println("-------------------------");
        head1 = new ListNode(1);
        head1.next = new ListNode(4);
        head1.next.next = new ListNode(5);

        head2 = new ListNode(1);
        head2.next = new ListNode(3);
        head2.next.next = new ListNode(4);

        head3 = new ListNode(2);
        head3.next = new ListNode(6);

        ListNode[] lists2 = {head1, head2, head3};
        ListNode.printListNode(sol.mergeKLists(lists2));
        System.out.println("-------------------------");

        head1 = new ListNode(-2);
        head1.next = new ListNode(-1);
        head1.next.next = new ListNode(-1);
        head1.next.next.next = new ListNode(-1);

        head2 = null;
        ListNode[] lists3 = {head1, head2};
        ListNode.printListNode(sol.mergeKLists(lists3));
    }
}
